package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.VectricsException;
import org.vectrics.common.html.Table;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidateCriteria;
import org.vectrics.recruiter.job.JobSearchCriteria;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

public class CandidateJobListTag extends VectricsTagSupport {

	private static final long serialVersionUID = 2628098331994936100L;
	
	static private Category log = Category.getInstance(CandidateJobListTag.class);
    private String candidateId = null;
    private HttpServletRequest request = null;
    private String showJobSelection = "false";
    private String compact = null;
    private String activeOnly = "false";
    private String tableWidth = "90%";
    

    private String getAddJobHref(Job job) {
    	String href = null;
    	href = request.getContextPath() + "/admin/user/addJob.do?userId=" + candidateId + "&jobId=" + job.getId();
    	return(href);
    }
	
    
	public String makeStartTag()  {
		StringBuffer html = new StringBuffer();
		
		log.debug("doStartTag() called");
		request = (HttpServletRequest)super.pageContext.getRequest();
		String contextPath = request.getContextPath();
		if (candidateId == null) {
			log.error("CandidateId property not set before rendering");
			throw new DataException("CandidateId property not set before rendering");
		}
		
		try {
			Long personId = new Long(candidateId);
			Person person = CoreServiceLocator.getPersonService().findPersonById(personId);
			

			if (this.showJobSelection.equals("true")) {
				JobSearchCriteria criteria = new JobSearchCriteria();
				criteria.addStatusTypeCode(JobStatusType.STATUS_POSTED);
				List jobs = RecruitServiceLocator.getJobProvider().findJobs(criteria, 0, 100);
				displayJobSelection(html, jobs);
			}
			else {
				JobCandidateCriteria criteria = new JobCandidateCriteria();
				criteria.setPerson(person);
				List jobCandidates = RecruitServiceLocator.getJobProvider().findJobCandidates(criteria, 0, 100);
				displayCandidateJobs(html, jobCandidates);
			}
		}
		catch (Exception de) {
			log.error("DataException finding jobs", de);
			throw new DataException("DataException finding jobs", de);
		}
		return(html.toString());
	}

		
	private void displayCandidateJobs(StringBuffer html, List jobCandidates) throws VectricsException {
		if (jobCandidates.size() == 0) {
			html.append("<font><i>No Jobs</i></font>");
		} else {
			Table table = new CandidateJobsTable(this.getAuthPerson(), this.getRequest(), jobCandidates);
			table.setWidth(tableWidth);
			html.append(table.toString());
		}
	}


	
	private void displayJobSelection(StringBuffer html, List activeJobs) {
		if (activeJobs.size() == 0) {
			html.append("<font><i>No Jobs</i></font>");
		}
		if (activeJobs.size() > 0) {
			Table table = new CandidateJobsTable(this.getAuthPerson(), this.getRequest(), activeJobs);
			table.setWidth(tableWidth);
/*
			Table table = new BorderTable();
			table.setWidth(tableWidth);
			Row row = table.addRow();
			Cell cell = row.addCell();
			
			cell.setClass("tableHdrBg");
			cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.company")));
			
			cell = row.addCell();
			cell.setClass("tableHdrBg");
			cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.job.title")));
			
			cell = row.addCell();
			cell.setClass("tableHdrBg");
			cell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.job.summary")));
			
			
			cell = row.addCell();
			cell.setClass("tableHdrBg");
			
			cell = row.addCell();
			cell.setClass("tableHdrBg");
			
			for (int i = 0; i < activeJobs.size(); i++) {
				Job job = (Job)activeJobs.get(i);
				row = table.addRow();
				if ((this.getActiveOnly().equals("false")) || (job.getCurrentStatus().getStatusType().getCode().equals(JobStatusType.STATUS_POSTED) == false)) {
					
					cell = row.addCell();
					cell.addElement(TextHelper.getFont("subFont", this.getCompanyName(job)));
					
					cell = row.addCell();
					cell.addElement(TextHelper.getFont("subFont", job.getTitle()));
					
					cell = row.addCell();
					String summary = job.getSummary();
					if ((summary == null) || (summary.trim().length() <= 0)) {
						summary = job.getDescription();
						if ((summary != null) && (summary.length() > 200)) {
							summary = summary.substring(0, 199) + " ...";
						}
					}
					if (summary != null)
						cell.addElement(TextHelper.getFont("subFont", summary));
					
					cell = row.addCell();
					A addLink = new A();
					addLink.setClass("subLink");
					addLink.setHref(getAddJobHref(job));
					addLink.addElement("select");
					cell.addElement(addLink);
					
					cell = row.addCell();
					A jobLink = new A();
					jobLink.setClass("subLink");
					jobLink.setHref(request.getContextPath() + "/admin/jobs/view.do?jobId=" + job.getId());
					jobLink.setOnClick("OpenLarge(this.href, '" + job.getId() + "'); return false");
					jobLink.addElement("job details");
					cell.addElement(jobLink);
				}
				
			}
				*/
			html.append(table.toString());
		}
	}
	

	/**
	 * @return Returns the candidateId.
	 */
	public String getCandidateId() {
		return candidateId;
	}

	/**
	 * @param candidateId The candidateId to set.
	 */
	public void setCandidateId(String candidateId) {
		this.candidateId = candidateId;
	}

	/**
	 * @return Returns the activeOnly.
	 */
	public String getActiveOnly() {
		return activeOnly;
	}

	/**
	 * @param activeOnly The activeOnly to set.
	 */
	public void setActiveOnly(String activeOnly) {
		this.activeOnly = activeOnly;
	}

	/**
	 * @return Returns the compact.
	 */
	public String getCompact() {
		return compact;
	}

	/**
	 * @param compact The compact to set.
	 */
	public void setCompact(String compact) {
		this.compact = compact;
	}

	/**
	 * @return Returns the showJobSelection.
	 */
	public String getShowJobSelection() {
		return showJobSelection;
	}

	/**
	 * @param showJobSelection The showJobSelection to set.
	 */
	public void setShowJobSelection(String showJobSelection) {
		this.showJobSelection = showJobSelection;
	}

}
